export class RemUtil {
    /* 设计图文档宽度 */
    private docWidth:number = 1920;
    constructor() {
        this._ruleJudge = this._ruleJudge.bind(this);
        this._addEventListener();
    }
    _addEventListener() {
        let doc = window.document;
        if (!doc.addEventListener) {
            return;
        }
        let resizeEvt = "orientationchange" in window ? "orientationchange" : "resize";
        window.addEventListener(resizeEvt, this._ruleJudge, false);
        doc.addEventListener("DOMContentLoaded", this._ruleJudge, false);
    }

    _ruleJudge() {
        let docWidth = this.docWidth;
        let doc = window.document;
        let docEl = doc.documentElement;
        let clientWidth = docEl.getBoundingClientRect().width;

        if (clientWidth < docWidth) {
            clientWidth = docWidth;
        }
        docEl.style.fontSize = (clientWidth / docWidth) * 100 + "px";
    }

    /**
     * 自适应窗口大小
     * @param {Number}  clientWidth 屏幕宽
     * @param {Number} docWidth 文档宽
     *  @returns {*} 无
     */
    resizeWidthAndHeight(clientWidth:number, docWidth:number) {
        let widthRate = clientWidth / docWidth;
        let body = document.getElementsByTagName("body")[0];

        body.style.setProperty("transform-origin", "left top");
        body.style.setProperty("transform", "scale(" + widthRate + ", 1)");
        body.style.setProperty("overflow", "visible");
    }
}
